home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / gdb / gdb_18s.zoo / symtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  32.5 KB  |  1,247 lines

  1. /* Symbol table lookup for the GNU debugger, GDB.
  2.    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. GDB is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the GDB General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute GDB,
  11. but only under the conditions described in the GDB General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with GDB so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17. In other words, go ahead and share GDB, but don't try to stop
  18. anyone else from sharing it farther.  Help stamp out software hoarding!
  19. */
  20.  
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "param.h"
  24.  
  25. #include <stdio.h>
  26. #include <obstack.h>
  27.  
  28. /* Allocate an obstack to hold objects that should be freed
  29.    when we load a new symbol table.
  30.    This includes the symbols made by dbxread
  31.    and the types that are not permanent.  */
  32.  
  33. struct obstack obstack1;
  34.  
  35. struct obstack *symbol_obstack = &obstack1;
  36.  
  37. /* These variables point to the objects
  38.    representing the predefined C data types.  */
  39.  
  40. struct type *builtin_type_void;
  41. struct type *builtin_type_char;
  42. struct type *builtin_type_short;
  43. struct type *builtin_type_int;
  44. struct type *builtin_type_long;
  45. struct type *builtin_type_unsigned_char;
  46. struct type *builtin_type_unsigned_short;
  47. struct type *builtin_type_unsigned_int;
  48. struct type *builtin_type_unsigned_long;
  49. struct type *builtin_type_float;
  50. struct type *builtin_type_double;
  51.  
  52. #ifdef atarist
  53. extern int gcc_mshort;
  54. #endif
  55.  
  56. /* Lookup the symbol table of a source file named NAME.  Try a couple
  57.    of variations if the first lookup doesn't work.  */
  58.  
  59. static struct symtab *
  60. lookup_symtab_1 (name)
  61.      char *name;
  62. {
  63.   register struct symtab *s;
  64.   register char *copy;
  65.   register char *slash;
  66.   extern char *strchr();
  67.  
  68.   for (s = symtab_list; s; s = s->next)
  69.     if (!strcmp (name, s->filename))
  70.       return s;
  71.  
  72.     if(!(slash = strchr (name, '/')))
  73.       slash = strchr (name, '\\');
  74.  
  75.   if (!slash)
  76.   {
  77.       int len = strlen(name);
  78.       
  79.       for (s = symtab_list; s; s = s->next)
  80.       {
  81.       int l = strlen (s->filename);
  82.  
  83.       if (s->filename[l - len -1] == *slash
  84.           && !strcmp (s->filename + l - len, name))
  85.         return s;
  86.       }
  87.   }
  88.   
  89.   return 0;
  90. }
  91.  
  92. struct symtab *
  93. lookup_symtab (name)
  94.      char *name;
  95. {
  96.   register struct symtab *s;
  97.   register char *copy;
  98.  
  99.   s = lookup_symtab_1 (name);
  100.   if (s) return s;
  101.  
  102.   /* If name not found as specified, see if adding ".c" helps.  */
  103.  
  104.   copy = (char *) alloca (strlen (name) + 3);
  105.   strcpy (copy, name);
  106.   strcat (copy, ".c");
  107.   s = lookup_symtab_1 (copy);
  108.   if (s) return s;
  109.  
  110.   /* We didn't find anything; die.  */
  111.   return 0;
  112. }
  113.  
  114. /* Lookup a typedef or primitive type named NAME,
  115.    visible in lexical block BLOCK.
  116.    If NOERR is nonzero, return zero if NAME is not suitably defined.  */
  117.  
  118. struct type *
  119. lookup_typename (name, block, noerr)
  120.      char *name;
  121.      struct block *block;
  122.      int noerr;
  123. {
  124.   register struct symbol *sym = lookup_symbol (name, block, VAR_NAMESPACE);
  125.   if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
  126.     {
  127.       if (!strcmp (name, "int"))
  128. #ifdef atarist
  129.     if (gcc_mshort)
  130.       return builtin_type_short;
  131.     else
  132. #endif
  133.     return builtin_type_int;
  134.       if (!strcmp (name, "long"))
  135.     return builtin_type_long;
  136.       if (!strcmp (name, "short"))
  137.     return builtin_type_short;
  138.       if (!strcmp (name, "char"))
  139.     return builtin_type_char;
  140.       if (!strcmp (name, "float"))
  141.     return builtin_type_float;
  142.       if (!strcmp (name, "double"))
  143.     return builtin_type_double;
  144.       if (!strcmp (name, "void"))
  145.     return builtin_type_void;
  146.  
  147.       if (noerr)
  148.     return 0;
  149.       error ("No type named %s.", name);
  150.     }
  151.   return SYMBOL_TYPE (sym);
  152. }
  153.  
  154. struct type *
  155. lookup_unsigned_typename (name)
  156.      char *name;
  157. {
  158.   if (!strcmp (name, "int"))
  159. #ifdef atarist
  160.     if (gcc_mshort)
  161.       return builtin_type_unsigned_short;
  162.     else
  163. #endif
  164.     return builtin_type_unsigned_int;
  165.   if (!strcmp (name, "long"))
  166.     return builtin_type_unsigned_long;
  167.   if (!strcmp (name, "short"))
  168.     return builtin_type_unsigned_short;
  169.   if (!strcmp (name, "char"))
  170.     return builtin_type_unsigned_char;
  171.   error ("No type named unsigned %s.", name);
  172. }
  173.  
  174. /* Lookup a structure type named "struct NAME",
  175.    visible in lexical block BLOCK.  */
  176.  
  177. struct type *
  178. lookup_struct (name, block)
  179.      char *name;
  180.      struct block *block;
  181. {
  182.   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
  183.   if (sym == 0)
  184.     error ("No struct type named %s.", name);
  185.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
  186.     error ("This context has union or enum %s, not a struct.", name);
  187.   return SYMBOL_TYPE (sym);
  188. }
  189.  
  190. /* Lookup a union type named "union NAME",
  191.    visible in lexical block BLOCK.  */
  192.  
  193. struct type *
  194. lookup_union (name, block)
  195.      char *name;
  196.      struct block *block;
  197. {
  198.   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
  199.   if (sym == 0)
  200.     error ("No union type named %s.", name);
  201.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
  202.     error ("This context has struct or enum %s, not a union.", name);
  203.   return SYMBOL_TYPE (sym);
  204. }
  205.  
  206. /* Lookup an enum type named "enum NAME",
  207.    visible in lexical block BLOCK.  */
  208.  
  209. struct type *
  210. lookup_enum (name, block)
  211.      char *name;
  212.      struct block *block;
  213. {
  214.   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
  215.   if (sym == 0)
  216.     error ("No enum type named %s.", name);
  217.   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
  218.     error ("This context has struct or union %s, not an enum.", name);
  219.   return SYMBOL_TYPE (sym);
  220. }
  221.  
  222. /* Given a type TYPE, return a type of pointers to that type.
  223.    May need to construct such a type if this is the first use.  */
  224.  
  225. struct type *
  226. lookup_pointer_type (type)
  227.      struct type *type;
  228. {
  229.   register struct type *ptype = TYPE_POINTER_TYPE (type);
  230.   if (ptype) return ptype;
  231.  
  232.   /* This is the first time anyone wanted a pointer to a TYPE.  */
  233.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  234.     ptype  = (struct type *) xmalloc (sizeof (struct type));
  235.   else
  236.     ptype  = (struct type *) obstack_alloc (symbol_obstack,
  237.                         sizeof (struct type));
  238.  
  239.   bzero (ptype, sizeof (struct type));
  240.   TYPE_TARGET_TYPE (ptype) = type;
  241.   TYPE_POINTER_TYPE (type) = ptype;
  242.   /* New type is permanent if type pointed to is permanent.  */
  243.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  244.     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
  245.   /* We assume the machine has only one representation for pointers!  */
  246.   TYPE_LENGTH (ptype) = sizeof (char *);
  247.   TYPE_CODE (ptype) = TYPE_CODE_PTR;
  248.   return ptype;
  249. }
  250.  
  251. /* Given a type TYPE, return a type of functions that return that type.
  252.    May need to construct such a type if this is the first use.  */
  253.  
  254. struct type *
  255. lookup_function_type (type)
  256.      struct type *type;
  257. {
  258.   register struct type *ptype = TYPE_FUNCTION_TYPE (type);
  259.   if (ptype) return ptype;
  260.  
  261.   /* This is the first time anyone wanted a function returning a TYPE.  */
  262.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  263.     ptype  = (struct type *) xmalloc (sizeof (struct type));
  264.   else
  265.     ptype  = (struct type *) obstack_alloc (symbol_obstack,
  266.                         sizeof (struct type));
  267.  
  268.   bzero (ptype, sizeof (struct type));
  269.   TYPE_TARGET_TYPE (ptype) = type;
  270.   TYPE_FUNCTION_TYPE (type) = ptype;
  271.   /* New type is permanent if type returned is permanent.  */
  272.   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
  273.     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
  274.   TYPE_LENGTH (ptype) = 1;
  275.   TYPE_CODE (ptype) = TYPE_CODE_FUNC;
  276.   TYPE_NFIELDS (ptype) = 0;
  277.   return ptype;
  278. }
  279.  
  280. #if 0
  281. /* Smash TYPE to be a type of pointers to TO_TYPE.
  282.    If TO_TYPE is not permanent and has no pointer-type yet,
  283.    record TYPE as its pointer-type.  */
  284.  
  285. void
  286. smash_to_pointer_type (type, to_type)
  287.      struct type *type, *to_type;
  288. {
  289.   int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM);
  290.  
  291.   bzero (type, sizeof (struct type));
  292.   TYPE_TARGET_TYPE (type) = to_type;
  293.   /* We assume the machine has only one representation for pointers!  */
  294.   TYPE_LENGTH (type) = sizeof (char *);
  295.   TYPE_CODE (type) = TYPE_CODE_PTR;
  296.  
  297.   if (type_permanent)
  298.     TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
  299.  
  300.   if (TYPE_POINTER_TYPE (to_type) == 0
  301.       && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM)
  302.       || type_permanent))
  303.     {
  304.       TYPE_POINTER_TYPE (to_type) = type;
  305.     }
  306. }
  307.  
  308. /* Smash TYPE to be a type of functions returning TO_TYPE.
  309.    If TO_TYPE is not permanent and has no function-type yet,
  310.    record TYPE as its function-type.  */
  311.  
  312. void
  313. smash_to_function_type (type, to_type)
  314.      struct type *type, *to_type;
  315. {
  316.   int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM);
  317.  
  318.   bzero (type, sizeof (struct type));
  319.   TYPE_TARGET_TYPE (type) = to_type;
  320.   TYPE_LENGTH (type) = 1;
  321.   TYPE_CODE (type) = TYPE_CODE_FUNC;
  322.   TYPE_NFIELDS (type) = 0;
  323.  
  324.   if (type_permanent)
  325.     TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
  326.  
  327.   if (TYPE_FUNCTION_TYPE (to_type) == 0
  328.       && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM)
  329.       || type_permanent))
  330.     {
  331.       TYPE_FUNCTION_TYPE (to_type) = type;
  332.     }
  333. }
  334. #endif
  335.  
  336. static struct symbol *lookup_block_symbol ();
  337. static int lookup_misc_func ();
  338.  
  339.  
  340. /* Find the definition for a specified symbol name NAME
  341.    in namespace NAMESPACE, visible from lexical block BLOCK.
  342.    Returns the struct symbol pointer, or zero if no symbol is found.  */
  343.  
  344. struct symbol *
  345. lookup_symbol (name, block, namespace)
  346.      char *name;
  347.      register struct block *block;
  348.      enum namespace namespace;
  349. {
  350.   register int i, n;
  351.   register struct symbol *sym;
  352.   register struct symtab *s;
  353.   struct blockvector *bv;
  354.  
  355.   /* Search specified block and its superiors.  */
  356.  
  357.   while (block != 0)
  358.     {
  359.       sym = lookup_block_symbol (block, name, namespace);
  360.       if (sym) return sym;
  361.       block = BLOCK_SUPERBLOCK (block);
  362.     }
  363.  
  364.   /* Now search all symtabs' global blocks.  */
  365.  
  366.   for (s = symtab_list; s; s = s->next)
  367.     {
  368.       bv = BLOCKVECTOR (s);
  369.       block = BLOCKVECTOR_BLOCK (bv, 0);
  370.       sym = lookup_block_symbol (block, name, namespace);
  371.       if (sym) return sym;
  372.     }
  373.  
  374.   /* Now search all symtabs' per-file blocks.
  375.      Not strictly correct, but more useful than an error.  */
  376.  
  377.   for (s = symtab_list; s; s = s->next)
  378.     {
  379.       bv = BLOCKVECTOR (s);
  380.       block = BLOCKVECTOR_BLOCK (bv, 1);
  381.       sym = lookup_block_symbol (block, name, namespace);
  382.       if (sym) return sym;
  383.     }
  384.   return 0;
  385. }
  386.  
  387. /* Look for a symbol in block BLOCK.  */
  388.  
  389. static struct symbol *
  390. lookup_block_symbol (block, name, namespace)
  391.      register struct block *block;
  392.      char *name;
  393.      enum namespace namespace;
  394. {
  395.   register int bot, top, inc;
  396.   register struct symbol *sym, *parameter_sym;
  397.  
  398.   top = BLOCK_NSYMS (block);
  399.   bot = 0;
  400.  
  401.   /* If the blocks's symbols were sorted, start with a binary search.  */
  402.  
  403.   if (BLOCK_SHOULD_SORT (block))
  404.     {
  405.       /* First, advance BOT to not far before
  406.      the first symbol whose name is NAME.  */
  407.  
  408.       while (1)
  409.     {
  410.       inc = (top - bot + 1);
  411.       /* No need to keep binary searching for the last few bits worth.  */
  412.       if (inc < 4)
  413.         break;
  414.       inc = (inc >> 1) + bot;
  415.       sym = BLOCK_SYM (block, inc);
  416.       if (SYMBOL_NAME (sym)[0] < name[0])
  417.         bot = inc;
  418.       else if (SYMBOL_NAME (sym)[0] > name[0])
  419.         top = inc;
  420.       else if (strcmp (SYMBOL_NAME (sym), name) < 0)
  421.         bot = inc;
  422.       else
  423.         top = inc;
  424.     }
  425.  
  426.       /* Now scan forward until we run out of symbols,
  427.      find one whose name is greater than NAME,
  428.      or find one we want.
  429.      If there is more than one symbol with the right name and namespace,
  430.      we return the first one.  dbxread.c is careful to make sure
  431.      that if one is a register then it comes first.  */
  432.  
  433.       top = BLOCK_NSYMS (block);
  434.       while (bot < top)
  435.     {
  436.       sym = BLOCK_SYM (block, bot);
  437.       inc = SYMBOL_NAME (sym)[0] - name[0];
  438.       if (inc == 0)
  439.         inc = strcmp (SYMBOL_NAME (sym), name);
  440.       if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
  441.         return sym;
  442.       if (inc > 0)
  443.         return 0;
  444.       bot++;
  445.     }
  446.       return 0;
  447.     }
  448.  
  449.   /* Here if block isn't sorted.
  450.      This loop is equivalent to the loop above,
  451.      but hacked greatly for speed.
  452.  
  453.      Note that parameter symbols do not always show up last in the
  454.      list; this loop makes sure to take anything else other than
  455.      parameter symbols first; it only uses parameter symbols as a
  456.      last resort.  Note that this only takes up extra computation
  457.      time on a match.  */
  458.  
  459.   parameter_sym = (struct symbol *) 0;
  460.   top = BLOCK_NSYMS (block);
  461.   inc = name[0];
  462.   while (bot < top)
  463.     {
  464.       sym = BLOCK_SYM (block, bot);
  465.       if (SYMBOL_NAME (sym)[0] == inc
  466.       && !strcmp (SYMBOL_NAME (sym), name)
  467.       && SYMBOL_NAMESPACE (sym) == namespace)
  468.     {
  469.       if (SYMBOL_CLASS (sym) == LOC_ARG
  470. #if 0
  471.           || SYMBOL_CLASS (sym) == LOC_REF_ARG
  472.           || SYMBOL_CLASS (sym) == LOC_REGPARM
  473. #endif
  474.           )
  475.         parameter_sym = sym;
  476.       else
  477.         return sym;
  478.     }
  479.       bot++;
  480.     }
  481.   return parameter_sym;        /* Will be 0 if not found. */
  482. }
  483.  
  484. /* Return the symbol for the function which contains a specified
  485.    lexical block, described by a struct block BL.  */
  486.  
  487. struct symbol *
  488. block_function (bl)
  489.      struct block *bl;
  490. {
  491.   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
  492.     bl = BLOCK_SUPERBLOCK (bl);
  493.  
  494.   return BLOCK_FUNCTION (bl);
  495. }
  496.  
  497. /* Subroutine of find_pc_line */
  498.  
  499. static struct symtab *
  500. find_pc_symtab (pc)
  501.      register CORE_ADDR pc;
  502. {
  503.   register struct block *b;
  504.   struct blockvector *bv;
  505.   register struct symtab *s;
  506.  
  507.   /* Search all symtabs for one whose file contains our pc */
  508.  
  509.   for (s = symtab_list; s; s = s->next)
  510.     {
  511.       bv = BLOCKVECTOR (s);
  512.       b = BLOCKVECTOR_BLOCK (bv, 0);
  513.       if (BLOCK_START (b) <= pc
  514.       && BLOCK_END (b) > pc)
  515.     break;
  516.     }
  517.   return s;
  518. }
  519.  
  520. /* Find the source file and line number for a given PC value.
  521.    Return a structure containing a symtab pointer, a line number,
  522.    and a pc range for the entire source line.
  523.    The value's .pc field is NOT the specified pc.
  524.    NOTCURRENT nonzero means, if specified pc is on a line boundary,
  525.    use the line that ends there.  Otherwise, in that case, the line
  526.    that begins there is used.  */
  527.  
  528. struct symtab_and_line
  529. find_pc_line (pc, notcurrent)
  530.      CORE_ADDR pc;
  531.      int notcurrent;
  532. {
  533.   struct symtab *s;
  534.   register struct linetable *l;
  535.   register int len;
  536.   register int i;
  537.   register struct linetable_entry *item;
  538.   struct symtab_and_line value;
  539.   struct blockvector *bv;
  540.  
  541.   /* Info on best line seen so far, and where it starts, and its file.  */
  542.  
  543.   int best_line = 0;
  544.   CORE_ADDR best_pc = 0;
  545.   CORE_ADDR best_end = 0;
  546.   struct symtab *best_symtab = 0;
  547.  
  548.   /* Store here the first line number
  549.      of a file which contains the line at the smallest pc after PC.
  550.      If we don't find a line whose range contains PC,
  551.      we will use a line one less than this,
  552.      with a range from the start of that file to the first line's pc.  */
  553.   int alt_line = 0;
  554.   CORE_ADDR alt_pc = 0;
  555.   struct symtab *alt_symtab = 0;
  556.  
  557.   /* Info on best line seen in this file.  */
  558.  
  559.   int prev_line;
  560.   CORE_ADDR prev_pc;
  561.  
  562.   /* Info on first line of this file.  */
  563.  
  564.   int first_line;
  565.   CORE_ADDR first_pc;
  566.  
  567.   /* If this pc is not from the current frame,
  568.      it is the address of the end of a call instruction.
  569.      Quite likely that is the start of the following statement.
  570.      But what we want is the statement containing the instruction.
  571.      Fudge the pc to make sure we get that.  */
  572.  
  573.   if (notcurrent) pc -= 1;
  574.  
  575.   s = find_pc_symtab (pc);
  576.   if (s == 0)
  577.     {
  578.       value.symtab = 0;
  579.       value.line = 0;
  580.       value.pc = pc;
  581.       value.end = 0;
  582.       return value;
  583.     }
  584.  
  585.   bv = BLOCKVECTOR (s);
  586.  
  587.   /* Look at all the symtabs that share this blockvector.
  588.      They all have the same apriori range, that we found was right;
  589.      but they have different line tables.  */
  590.  
  591.   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
  592.     {
  593.       /* Find the best line in this symtab.  */
  594.       l = LINETABLE (s);
  595.       len = l->nitems;
  596.       prev_line = -1;
  597.       first_line = -1;
  598.       for (i = 0; i < len; i++)
  599.     {
  600.       item = &(l->item[i]);
  601.       
  602.       if (first_line < 0)
  603.         {
  604.           first_line = item->line;
  605.           first_pc = item->pc;
  606.         }
  607.       /* Return the last line that did not start after PC.  */
  608.       if (pc >= item->pc)
  609.         {
  610.           prev_line = item->line;
  611.           prev_pc = item->pc;
  612.         }
  613.       else
  614.         break;
  615.     }
  616.  
  617.       /* Is this file's best line closer than the best in the other files?
  618.      If so, record this file, and its best line, as best so far.  */
  619.       if (prev_line >= 0 && prev_pc > best_pc)
  620.     {
  621.       best_pc = prev_pc;
  622.       best_line = prev_line;
  623.       best_symtab = s;
  624.       if (i < len)
  625.         best_end = item->pc;
  626.       else
  627.         best_end = 0;
  628.     }
  629.       /* Is this file's first line closer than the first lines of other files?
  630.      If so, record this file, and its first line, as best alternate.  */
  631.       if (first_line >= 0 && first_pc > pc
  632.       && (alt_pc == 0 || first_pc < alt_pc))
  633.     {
  634.       alt_pc = first_pc;
  635.       alt_line = first_line;
  636.       alt_symtab = s;
  637.     }
  638.     }
  639.   if (best_symtab == 0)
  640.     {
  641.       value.symtab = alt_symtab;
  642.       value.line = alt_line - 1;
  643.       value.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0));
  644.       value.end = alt_pc;
  645.     }
  646.   else
  647.     {
  648.       value.symtab = best_symtab;
  649.       value.line = best_line;
  650.       value.pc = best_pc;
  651.       value.end = (best_end ? best_end
  652.            : (alt_pc ? alt_pc
  653.               : BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0))));
  654.     }
  655.   return value;
  656. }
  657.  
  658. static int find_line_common ();
  659.  
  660. /* Find the PC value for a given source file and line number.
  661.    Returns zero for invalid line number.
  662.    The source file is specified with a struct symtab.  */
  663.  
  664. CORE_ADDR
  665. find_line_pc (symtab, line)
  666.      struct symtab *symtab;
  667.      int line;
  668. {
  669.   register struct linetable *l;
  670.   register int Index;
  671.   int dummy;
  672.  
  673.   if (symtab == 0)
  674.     return 0;
  675.   l = LINETABLE (symtab);
  676.   Index = find_line_common(l, line, &dummy);
  677.   return (Index >= 0) ? l->item[Index].pc : 0;
  678. }
  679.  
  680. /* Find the range of pc values in a line.
  681.    Store the starting pc of the line into *STARTPTR
  682.    and the ending pc (start of next line) into *ENDPTR.
  683.    Returns 1 to indicate success.
  684.    Returns 0 if could not find the specified line.  */
  685.  
  686. int
  687. find_line_pc_range (symtab, thisline, startptr, endptr)
  688.      struct symtab *symtab;
  689.      int thisline;
  690.      CORE_ADDR *startptr, *endptr;
  691. {
  692.   register struct linetable *l;
  693.   register int Index;
  694.   int exact_match;        /* did we get an exact linenumber match */
  695.   register CORE_ADDR prev_pc;
  696.   CORE_ADDR last_pc;
  697.  
  698.   if (symtab == 0)
  699.     return 0;
  700.  
  701.   l = LINETABLE (symtab);
  702.   Index = find_line_common (l, thisline, &exact_match);
  703.   if (Index >= 0)
  704.     {
  705.       *startptr = l->item[Index].pc;
  706.       /* If we have not seen an entry for the specified line,
  707.      assume that means the specified line has zero bytes.  */
  708.       if (!exact_match || Index == l->nitems-1)
  709.     *endptr = *startptr;
  710.       else
  711.     /* Perhaps the following entry is for the following line.
  712.        It's worth a try.  */
  713.     if (Index+1 < l->nitems && l->item[Index+1].line == thisline + 1)
  714.       *endptr = l->item[Index+1].pc;
  715.     else
  716.       *endptr = find_line_pc (symtab, thisline+1);
  717.       return 1;
  718.     }
  719.  
  720.   return 0;
  721. }
  722.  
  723. /* Given a line table and a line number, return the index into the line
  724.    table for the pc of the nearest line whose number is >= the specified one.
  725.    Return 0 if none is found.  The value is never zero is it is an index.
  726.  
  727.    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
  728.  
  729. static int
  730. find_line_common (l, lineno, exact_match)
  731.      register struct linetable *l;
  732.      register int lineno;
  733.      int *exact_match;
  734. {
  735.   register int i;
  736.   register int len;
  737.  
  738.   /* BEST is the smallest linenumber > LINENO so far seen,
  739.      or 0 if none has been seen so far.
  740.      BEST_INDEX identifies the item for it.  */
  741.  
  742.   int best_index = -1;
  743.   int best = 0;
  744.  
  745.   int nextline = -1;
  746.  
  747.   if (lineno <= 0)
  748.     return -1;
  749.  
  750.   len = l->nitems;
  751.   for (i = 0; i < len; i++)
  752.     {
  753.       register struct linetable_entry *item = &(l->item[i]);
  754.  
  755.       if (item->line == lineno)
  756.     {
  757.       *exact_match = 1;
  758.       return i;
  759.     }
  760.  
  761.       if (item->line > lineno && (best == 0 || item->line < best))
  762.     {
  763.       best = item->line;
  764.       best_index = i;
  765.     }
  766.     }
  767.  
  768.   /* If we got here, we didn't get an exact match.  */
  769.  
  770.   *exact_match = 0;
  771.   return best_index;
  772. }
  773.  
  774. int
  775. find_pc_line_pc_range (pc, startptr, endptr)
  776.      CORE_ADDR pc;
  777.      CORE_ADDR *startptr, *endptr;
  778. {
  779.   struct symtab_and_line sal;
  780.   sal = find_pc_line (pc, 0);
  781.   *startptr = sal.pc;
  782.   *endptr = sal.end;
  783.   return sal.symtab != 0;
  784. }
  785.  
  786. /* Parse a string that specifies a line number.
  787.    Pass the address of a char * variable; that variable will be
  788.    advanced over the characters actually parsed.
  789.  
  790.    The string can be:
  791.  
  792.    LINENUM -- that line number in current file.  PC returned is 0.
  793.    FILE:LINENUM -- that line in that file.  PC returned is 0.
  794.    FUNCTION -- line number of openbrace of that function.
  795.       PC returned is the start of the function.
  796.    FILE:FUNCTION -- likewise, but prefer functions in that file.
  797.    *EXPR -- line in which address EXPR appears.
  798.  
  799.    FUNCTION may be an undebuggable function found in misc_function_vector.
  800.  
  801.    If the argument FUNFIRSTLINE is nonzero, we want the first line
  802.    of real code inside a function when a function is specified.
  803.  
  804.    DEFAULT_SYMTAB specifies the file to use if none is specified.
  805.    It defaults to current_source_symtab.
  806.    DEFAULT_LINE specifies the line number to use for relative
  807.    line numbers (that start with signs).  Defaults to current_source_line.
  808.  
  809.    Note that it is possible to return zero for the symtab
  810.    if no file is validly specified.  Callers must check that.
  811.    Also, the line number returned may be invalid.  */
  812.  
  813. struct symtab_and_line
  814. decode_line_1 (argptr, funfirstline, default_symtab, default_line)
  815.      char **argptr;
  816.      int funfirstline;
  817.      struct symtab *default_symtab;
  818.      int default_line;
  819. {
  820.   struct symtab_and_line value;
  821.   register char *p, *p1;
  822.   register struct symtab *s;
  823.   register struct symbol *sym;
  824.   register CORE_ADDR pc;
  825.   register int i;
  826.   char *copy;
  827.  
  828.   /* Defaults have defaults.  */
  829.  
  830.   if (default_symtab == 0)
  831.     {
  832.       default_symtab = current_source_symtab;
  833.       default_line = current_source_line;
  834.     }
  835.  
  836.   /* See if arg is *PC */
  837.  
  838.   if (**argptr == '*')
  839.     {
  840.       (*argptr)++;
  841.       pc = parse_and_eval_address_1 (argptr);
  842.       value = find_pc_line (pc, 0);
  843.       value.pc = pc;
  844.       return value;
  845.     }
  846.  
  847.   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
  848.  
  849.   s = 0;
  850.  
  851.   for (p = *argptr; *p; p++)
  852.     {
  853.       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
  854.     break;
  855.     }
  856.   while (p[0] == ' ' || p[0] == '\t') p++;
  857.  
  858.   if (p[0] == ':')
  859.     {
  860.       /* Extract the file name.  */
  861.       p1 = p;
  862.       while (p != *argptr && p[-1] == ' ') --p;
  863.       copy = (char *) alloca (p - *argptr + 1);
  864.       bcopy (*argptr, copy, p - *argptr);
  865.       copy[p - *argptr] = 0;
  866.  
  867.       /* Find that file's data.  */
  868.       s = lookup_symtab (copy);
  869.       if (s == 0)
  870.     {
  871.       if (symtab_list == 0)
  872.         error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  873.       error ("No source file named %s.", copy);
  874.     }
  875.  
  876.       /* Discard the file name from the arg.  */
  877.       p = p1 + 1;
  878.       while (*p == ' ' || *p == '\t') p++;
  879.       *argptr = p;
  880.     }
  881.  
  882.   /* S is specified file's symtab, or 0 if no file specified.
  883.      arg no longer contains the file name.  */
  884.  
  885.   /* Check whether arg is all digits (and sign) */
  886.  
  887.   p = *argptr;
  888.   if (*p == '-' || *p == '+') p++;
  889.   while (*p >= '0' && *p <= '9')
  890.     p++;
  891.  
  892.   if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
  893.     {
  894.       /* We found a token consisting of all digits -- at least one digit.  */
  895.       enum sign {none, plus, minus} sign = none;
  896.  
  897.       /* This is where we need to make sure that we have good defaults.
  898.      We must guarrantee that this section of code is never executed
  899.      when we are called with just a function name, since
  900.      select_source_symtab calls us with such an argument  */
  901.  
  902.       if (s == 0 && default_symtab == 0)
  903.     {
  904.       if (symtab_list == 0)
  905.         error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  906. /*      select_source_symtab (0); */
  907.       default_symtab = current_source_symtab;
  908.       default_line = current_source_line;
  909.     }
  910.       if (**argptr == '+')
  911.     sign = plus, (*argptr)++;
  912.       else if (**argptr == '-')
  913.     sign = minus, (*argptr)++;
  914.       value.line = atoi (*argptr);
  915.       switch (sign)
  916.     {
  917.     case plus:
  918.       if (p == *argptr)
  919.         value.line = 5;
  920.       if (s == 0)
  921.         value.line = default_line + value.line;
  922.       break;
  923.     case minus:
  924.       if (p == *argptr)
  925.         value.line = 15;
  926.       if (s == 0)
  927.         value.line = default_line - value.line;
  928.       else
  929.         value.line = 1;
  930.       break;
  931.     }
  932.  
  933.       while (*p == ' ' || *p == '\t') p++;
  934.       *argptr = p;
  935.       if (s == 0)
  936.     s = default_symtab;
  937.       value.symtab = s;
  938.       value.pc = 0;
  939.       return value;
  940.     }
  941.  
  942.   /* Arg token is not digits => try it as a function name
  943.      Find the next token (everything up to end or next whitespace).  */
  944.   p = *argptr;
  945.   while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
  946.   copy = (char *) alloca (p - *argptr + 1);
  947.   bcopy (*argptr, copy, p - *argptr);
  948.   copy[p - *argptr] = 0;
  949.   while (*p == ' ' || *p == '\t') p++;
  950.   *argptr = p;
  951.  
  952.   /* Look up that token as a function.
  953.      If file specified, use that file's per-file block to start with.  */
  954.  
  955.   sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0,
  956.                VAR_NAMESPACE);
  957.  
  958.   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  959.     {
  960.       /* Arg is the name of a function */
  961.       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
  962.       if (funfirstline)
  963.     SKIP_PROLOGUE (pc);
  964.       value = find_pc_line (pc, 0);
  965.       value.pc = (value.end && value.pc != pc) ? value.end : pc;
  966.       return value;
  967.     }
  968.  
  969.   if (sym)
  970.     error ("%s is not a function.", copy);
  971.  
  972.   if ((i = lookup_misc_func (copy)) < 0)
  973.     error ("Function %s not defined.", copy);
  974.   else
  975.     {
  976.       value.symtab = 0;
  977.       value.line = 0;
  978.       value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
  979.       if (funfirstline)
  980.     SKIP_PROLOGUE (value.pc);
  981.       return value;
  982.     }
  983.  
  984.   if (symtab_list == 0)
  985.     error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
  986.   error ("Function %s not defined.", copy);
  987. }
  988.  
  989. struct symtab_and_line
  990. decode_line_spec (string, funfirstline)
  991.      char *string;
  992.      int funfirstline;
  993. {
  994.   struct symtab_and_line sal;
  995.   if (string == 0)
  996.     error ("Empty line specification.");
  997.   sal = decode_line_1 (&string, funfirstline,
  998.                current_source_symtab, current_source_line);
  999.   if (*string)
  1000.     error ("Junk at end of line specification: %s", string);
  1001.   return sal;
  1002. }
  1003.  
  1004. /* Return the index of misc function named NAME.  */
  1005.  
  1006. static
  1007. int lookup_misc_func (name)
  1008. register char *name;
  1009. {
  1010.   register int i;
  1011.  
  1012.   for (i = 0; i < misc_function_count; i++)
  1013.     if (!strcmp (misc_function_vector[i].name, name))
  1014.       return i;
  1015.   return -1;        /* not found */
  1016. }
  1017.  
  1018. static void
  1019. sources_info ()
  1020. {
  1021.   register struct symtab *s;
  1022.   register int column = 0;
  1023.  
  1024.   if (symtab_list == 0)
  1025.     {
  1026.      printf_filtered ("No symbol table is loaded.\n");
  1027.       return;
  1028.     }
  1029.  printf_filtered ("Source files for which symbol table is known:\n");
  1030.   for (s = symtab_list; s; s = s->next)
  1031.     {
  1032.       if (column != 0 && column + strlen (s->filename) >= 70)
  1033.     {
  1034.      printf_filtered ("\n");
  1035.       column = 0;
  1036.     }
  1037.       else if (column != 0)
  1038.     {
  1039.      printf_filtered (" ");
  1040.       column++;
  1041.     }
  1042.      printf_filtered ("%s", s->filename);
  1043.       column += strlen (s->filename);
  1044.       if (s->next)
  1045.     {
  1046.      printf_filtered (",");
  1047.       column++;
  1048.     }
  1049.     }
  1050.  printf_filtered ("\n");
  1051. }
  1052.  
  1053. /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
  1054.    If CLASS is zero, list all symbols except functions and type names.
  1055.    If CLASS is 1, list only functions.
  1056.    If CLASS is 2, list only type names.  */
  1057.  
  1058. #define MORE  \
  1059. { print_count++;        \
  1060.   if (print_count >= 21)    \
  1061.     {printf_filtered ("--Type Return to print more--");    \
  1062.       print_count = 0;        \
  1063.       fflush (stdout);        \
  1064.       read_line (); } }
  1065.  
  1066. static void sort_block_syms ();
  1067.  
  1068. static void
  1069. list_symbols (regexp, class)
  1070.      char *regexp;
  1071.      int class;
  1072. {
  1073.   register struct symtab *s;
  1074.   register struct blockvector *bv;
  1075.   struct blockvector *prev_bv = 0;
  1076.   register struct block *b;
  1077.   register int i, j;
  1078.   register struct symbol *sym;
  1079.   char *val;
  1080.   int found_in_file;
  1081.   static char *classnames[]
  1082.     = {"variable", "function", "type"};
  1083.   int print_count = 0;
  1084.  
  1085.   if (regexp)
  1086.     if (val = (char *) re_comp (regexp))
  1087.       error ("Invalid regexp: %s", val);
  1088.  
  1089.  printf_filtered (regexp
  1090.       ? "All %ss matching regular expression \"%s\":\n"
  1091.       : "All defined %ss:\n",
  1092.       classnames[class],
  1093.       regexp);
  1094.  
  1095.   for (s = symtab_list; s; s = s->next)
  1096.     {
  1097.       found_in_file = 0;
  1098.       bv = BLOCKVECTOR (s);
  1099.       /* Often many files share a blockvector.
  1100.      Scan each blockvector only once so that
  1101.      we don't get every symbol many times.
  1102.      It happens that the first symtab in the list
  1103.      for any given blockvector is the main file.  */
  1104.       if (bv != prev_bv)
  1105.     for (i = 0; i < 2; i++)
  1106.       {
  1107.         b = BLOCKVECTOR_BLOCK (bv, i);
  1108.         /* Skip the sort if this block is always sorted.  */
  1109.         if (!BLOCK_SHOULD_SORT (b))
  1110.           sort_block_syms (b);
  1111.         for (j = 0; j < BLOCK_NSYMS (b); j++)
  1112.           {
  1113.         QUIT;
  1114.         sym = BLOCK_SYM (b, j);
  1115.         if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
  1116.             && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
  1117.              && SYMBOL_CLASS (sym) != LOC_BLOCK)
  1118.             || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
  1119.             || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)))
  1120.           {
  1121.             if (!found_in_file)
  1122.               {
  1123.         printf_filtered ("\nFile %s:\n", s->filename);
  1124.             print_count += 2;
  1125.               }
  1126.             found_in_file = 1;
  1127.             MORE;
  1128.             if (class != 2 && i == 1)
  1129.              printf_filtered ("static ");
  1130.             if (class == 2
  1131.             && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
  1132.              printf_filtered ("typedef ");
  1133.  
  1134.             type_print (SYMBOL_TYPE (sym),
  1135.                 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  1136.                  ? "" : SYMBOL_NAME (sym)),
  1137.                 stdout, 0);
  1138.             if (class == 2
  1139.             && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE
  1140.             && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0
  1141.                 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))),
  1142.                         SYMBOL_NAME (sym))))
  1143.              printf_filtered (" %s", SYMBOL_NAME (sym));
  1144.            printf_filtered (";\n");
  1145.           }
  1146.           }
  1147.       }
  1148.       prev_bv = bv;
  1149.     }
  1150. }
  1151.  
  1152. static void
  1153. variables_info (regexp)
  1154.      char *regexp;
  1155. {
  1156.   list_symbols (regexp, 0);
  1157. }
  1158.  
  1159. static void
  1160. functions_info (regexp)
  1161.      char *regexp;
  1162. {
  1163.   list_symbols (regexp, 1);
  1164. }
  1165.  
  1166. static void
  1167. types_info (regexp)
  1168.      char *regexp;
  1169. {
  1170.   list_symbols (regexp, 2);
  1171. }
  1172.  
  1173. /* Call sort_block_syms to sort alphabetically the symbols of one block.  */
  1174.  
  1175. static int
  1176. compare_symbols (s1, s2)
  1177.      struct symbol **s1, **s2;
  1178. {
  1179.   /* Names that are less should come first.  */
  1180.   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
  1181.   if (namediff != 0) return namediff;
  1182.   /* For symbols of the same name, registers should come first.  */
  1183.   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
  1184.       - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
  1185. }
  1186.  
  1187. static void
  1188. sort_block_syms (b)
  1189.      register struct block *b;
  1190. {
  1191.   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
  1192.      sizeof (struct symbol *), compare_symbols);
  1193. }
  1194.  
  1195. /* Initialize the standard C scalar types.  */
  1196.  
  1197. static
  1198. struct type *
  1199. init_type (code, length, uns, name)
  1200.      enum type_code code;
  1201.      int length, uns;
  1202.      char *name;
  1203. {
  1204.   register struct type *type;
  1205.  
  1206.   type = (struct type *) xmalloc (sizeof (struct type));
  1207.   bzero (type, sizeof *type);
  1208.   TYPE_CODE (type) = code;
  1209.   TYPE_LENGTH (type) = length;
  1210.   TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
  1211.   TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
  1212.   TYPE_NFIELDS (type) = 0;
  1213.   TYPE_NAME (type) = name;
  1214.  
  1215.   return type;
  1216. }
  1217.  
  1218. void
  1219. initialize_symtab ()
  1220. {
  1221.   add_info ("variables", variables_info,
  1222.         "All global and static variable names, or those matching REGEXP.");
  1223.   add_info ("functions", functions_info,
  1224.         "All function names, or those matching REGEXP.");
  1225.   add_info ("types", types_info,
  1226.         "All types names, or those matching REGEXP.");
  1227.   add_info ("sources", sources_info,
  1228.         "Source files in the program.");
  1229.  
  1230.   obstack_init (symbol_obstack);
  1231.  
  1232.   builtin_type_void = init_type (TYPE_CODE_VOID, 0, 0, "void");
  1233.  
  1234.   builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
  1235.   builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
  1236.  
  1237.   builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
  1238.   builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
  1239.   builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
  1240.   builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
  1241.  
  1242.   builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
  1243.   builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
  1244.   builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
  1245.   builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
  1246. }
  1247.